package com.cq.hd.app.controller;

import com.cq.hd.common.annotation.LoginUser;
import com.cq.hd.common.entity.AppLoginUser;
import com.cq.hd.common.entity.BusinessInfo;
import com.cq.hd.common.pagination.Page;
import com.cq.hd.common.response.Result;
import com.cq.hd.common.response.ResultGenerator;
import com.cq.hd.member.api.TbMerchantApi;
import com.cq.hd.member.api.TbMerchantEnterApplyApi;
import com.cq.hd.member.api.dto.AppMerchantDetailDto;
import com.cq.hd.member.api.dto.AppMerchantPageDto;
import com.cq.hd.member.api.vo.AppMerchantDetailVo;
import com.cq.hd.member.api.vo.AppMerchantPageVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

@RestController
@RequestMapping("/merchant")
@Api(tags = "商户相关接口")
public class MerchantController {

    @Autowired
    private TbMerchantApi merchantApi;

    @Autowired
    private TbMerchantEnterApplyApi merchantEnterApplyApi;

    @ApiOperation(value = "商家审核商户个数")
    @ApiResponse(response = Integer.class, code = 200, message = "接口返回对象参数")
    @GetMapping("/getAuditCount")
    public Result<Integer> getAuditCount(@ApiIgnore @LoginUser AppLoginUser appLoginUser) {
        BusinessInfo businessInfo = appLoginUser.getUserInfo().getBusinessInfo();
        if (businessInfo == null || businessInfo.getId() == 0) {
            return ResultGenerator.success(0);
        }
        return merchantEnterApplyApi.getAuditCount(businessInfo.getId());
    }

    @ApiOperation(value = "商户列表")
    @ApiImplicitParam(name = "appMerchantPageDto", value = "实体", required = true, paramType = "query", dataType = "AppMerchantPageDto")
    @ApiResponse(response = AppMerchantPageVo.class, code = 200, message = "接口返回对象参数")
    @GetMapping("/page")
    public Result<Page<AppMerchantPageVo>> pageAppMerchant(AppMerchantPageDto appMerchantPageDto, @ApiIgnore @LoginUser AppLoginUser appLoginUser) {
        appMerchantPageDto.setUserId(appLoginUser.getUserId());
        BusinessInfo businessInfo = appLoginUser.getUserInfo().getBusinessInfo();
        if (businessInfo != null) {
            appMerchantPageDto.setBusinessId(businessInfo.getId());
        }

        return merchantApi.pageAppMerchant(appMerchantPageDto);
    }

    @ApiOperation(value = "商户详情")
    @ApiImplicitParam(name = "appMerchantDetailDto", value = "实体", required = true, paramType = "query", dataType = "AppMerchantDetailDto")
    @ApiResponse(response = AppMerchantDetailVo.class, code = 200, message = "接口返回对象参数")
    @GetMapping("/detail")
    public Result<AppMerchantDetailVo> detailAppMerchant(AppMerchantDetailDto appMerchantDetailDto, @ApiIgnore @LoginUser AppLoginUser appLoginUser) {
        appMerchantDetailDto.setUserId(appLoginUser.getUserId());
        BusinessInfo businessInfo = appLoginUser.getUserInfo().getBusinessInfo();
        if (businessInfo != null) {
            appMerchantDetailDto.setBusinessId(businessInfo.getId());
        }

        return merchantApi.detailAppMerchant(appMerchantDetailDto);
    }

}
